package client;


import gui.Survey_GUI;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * 		Credit Card
 * 		used to validate and save credit card details
 */
public class CreditCard implements Serializable
{
	/**
	 * Credit Card Number
	 */
	private String cardNumber;
	
	/**
	 * Credit Card month expired date
	 */
	public String expireMonth;
	
	/**
	 * Credit Card year expired date
	 */
	public String expireYear;
	
	/**
	 * user id
	 */
    public Integer uid=-1;
	/**
	 * constructor
	 * Sets credit card parameters
	 * @param num sets the credit card number
	 * @param expM sets he credit card expired month
	 * @param expY sets he credit card expired year
	 */
	public CreditCard(String num, String expM,String expY)
	{
		cardNumber = num;
		expireMonth = expM;
		expireYear=expY;
		uid=LogIn.usr.getID();
	}
	/**
	 * constructor
	 * @param id - set uid
	 */
	public CreditCard(int id) 
	{
		cardNumber = null;
		expireMonth = null;
		expireYear=null;
		uid=id;
	}
	/**
	 *constructor
	 * @param id - set the login uid 
	 */
	public CreditCard() 
	{
		cardNumber = null;
		expireMonth = null;
		expireYear=null;
		uid=LogIn.usr.getID();
	}

	/**
	 * Validates credit card number
	 * @return 1 if credit card number legal, else 0
	 */
	public int validateCreditCardNumer()
	{	
		if(cardNumber.contains("-"))
		{
			if(cardNumber.charAt(4)!='-' || cardNumber.charAt(9)!='-' || cardNumber.charAt(14)!='-')
				return 0;
			if(cardNumber.length()!=19)
				return 0;
			String saveOld=cardNumber;
			String str=(cardNumber.substring(0, 4)+cardNumber.substring(5,9)+cardNumber.substring(10,14)+cardNumber.substring(15,19));
			for(int i=0;i<str.length();i++)
			{
				if(!Character.isDigit(str.charAt(i)))
					return 0;
			}
			cardNumber=saveOld;
			return 1;
		}
		else
		{
			if(cardNumber.length()==16)
			{
				for(int i=0;i<cardNumber.length();i++)
					if(!Character.isDigit(cardNumber.charAt(i)))
						return 0;
				String str=(cardNumber.substring(0, 4)+"-"+cardNumber.substring(4,8)+"-"+cardNumber.substring(8,12)+"-"+cardNumber.substring(12,16));
				setcardNumber(str);
				return 1;
			}
			
		}
		return 0;
	}
	
	/**
	 * Validates credit card expiration date
	 * @return 1 if expire date legal, else 0
	 */
	public int validateExpireDate()
	{
		SimpleDateFormat m=new SimpleDateFormat("MM");
		SimpleDateFormat y=new SimpleDateFormat("yyyy");
		Calendar c=Calendar.getInstance();
		int month=Integer.parseInt(m.format(c.getTime()));
		int year=Integer.parseInt(y.format(c.getTime()));
		
		if(Integer.parseInt(expireYear)<=year && Integer.parseInt(expireMonth)<month)
			return 0;
		else if(Integer.parseInt(expireYear)<year)
			return 0;
		else return 1;
	}
	
	/**
	 * Sets credit card number
	 * @param num credit card number
	 */
	public void setcardNumber(String num)
	{cardNumber = num;}
	
	/**
	 * Sets credit expire Month date
	 * @param num Month
	 */
	public void setexpireMonth(String num)
	{expireMonth = num;}
	
	/**
	 * Sets credit expire Year date
	 * @param num Year
	 */
	public void setexpireYear(String num)
	{expireYear = num;}
	
	/**
	 * Gets credit card number
	 * @return credit card number
	 */
	public String getcardNumber()
	{return cardNumber;}
	
	/**
	 * Gets credit card expiration date
	 * @return credit card expiration date
	 */
	public String getexpirationDate()
	{
		if(expireMonth==null)
		return null;
		String str=expireMonth+"/"+expireYear;
		return str;
	}

	/**
	 * create new credit card in DB
	 * @return 1 - if saved , 0 - if failed
	 */
	public int submitNew()
	{
		ObjHandle.sendToServer(this);
		int i= (Integer) Main.clie.getResult();
		return i;
	}
	
	/**
	 * @returns String array of the current year and 4 next years to come 
	 */
	public String[] getYears()
	{
		SimpleDateFormat d=new SimpleDateFormat("yyyy");
		Calendar c=Calendar.getInstance();	
		String years[]=new String[5];
		int j=0;
		for(int i=0;i<years.length;i++)
		{	
			c.set(c.get(Calendar.YEAR)+j, c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
			j=1;
			years[i]=d.format(c.getTime());
		}
		return years;
	}
	
	/**
	 * @returns instance of the credit card by it's uid & cardnumber 
	 */
	public CreditCard getSelectedCreditCard() 
	{	
		CreditCard cc=null;
		ObjHandle.sendToServer(this);
		cc=(CreditCard) Main.clie.getResult();
		return cc;
	}
	
	/**
	 * @returns (String array)- non expired credit cards numbers of a logged in user 
	 */
	public String[] getUpdatedCreditCardNumbers() 
	{	
		String []sn=new String[2];
		String [] cardnum=null;
		sn[0]="getUpdatedCreditCardNumbers";
		sn[1]=uid.toString();
		ObjHandle.sendToServer(sn);
		cardnum=(String[]) Main.clie.getResult();
		return cardnum;
	}
}